import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import viteCompression from "vite-plugin-compression"
const p = ({ mode }: any) => {
  const processEnv = loadEnv(mode, process.cwd())
  return defineConfig({
    plugins: [vue(),viteCompression()],
    define: {
      'process.env': loadEnv(mode, process.cwd())
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '~': path.resolve(__dirname, "public"),
        // Fix: You are runing the esm-bundler of vue-i18n. It is recommended to configure your
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      },
      extensions: [".ts", ".tsx"]
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: "@import '@/assets/css/style.scss';"
        }
      }
    },
    base: './',
    build: {
      chunkSizeWarningLimit: 500,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules/')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
            if (id.includes('node_modules\\')) {
              return id.toString().split('node_modules\\')[1].split('\\')[0].toString()
            }
          },
          chunkFileNames: (chunkInfo) => {
            const facadeModuleId = chunkInfo.facadeModuleId
              ? chunkInfo.facadeModuleId.split('/')
              : [];
            const fileName =
              facadeModuleId[facadeModuleId.length - 2] || '[name]';
            return `js/${fileName}/[name].[hash].js`;
          }
        },
        
      }
    },
    server: {
      host: '0.0.0.0',
      open: true, // 在服务器启动时自动在浏览器中打开应用程序
      //host: 'localhost',  // 指定服务器主机名
      port: 8080, // 指定服务器端口
      proxy: { // 为开发服务器配置自定义代理规则
        '/dev': {
          target: processEnv.VITE_LOCATION_ORIGIN,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev/, "")
        },
      }
    }
  })
}

// https://vitejs.dev/config/
export default p
